// SPDX-License-Identifier: MIT
pragma solidity ^0.8.8;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

// Why is this a library and not abstract?
// Why not an interface?
library PriceConverter {
    // We could make this public, but then we'd have to deploy it
    function getPrice(
        AggregatorV3Interface priceFeed
    ) internal view returns (uint256) {
        (, int256 answer, , , ) = priceFeed.latestRoundData();
        // ETH/USD rate in 18 digit
        return uint256(answer * 10000000000);
        // or (Both will do the same thing)
        // return uint256(answer * 1e10); // 1* 10 ** 10 == 10000000000
    }

    // 1000000000
    function getConversionRate(
        uint256 ethAmount,
        AggregatorV3Interface priceFeed
    ) internal view returns (uint256) {
        uint256 ethPrice = getPrice(priceFeed);
        uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
        // or (Both will do the same thing)
        // uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1e18; // 1 * 10 ** 18 == 1000000000000000000
        // the actual ETH/USD conversion rate, after adjusting the extra 0s.
        return ethAmountInUsd;
    }
}

// pragma solidity ^0.8.7;

// import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

// library PriceConverter {
//   function getPrice(AggregatorV3Interface priceFeed)
//     internal
//     view
//     returns (uint256)
//   {
//     (, int256 answer, , , ) = priceFeed.latestRoundData();
//     // ETH/USD rate in 18 digit
//     return uint256(answer * 10000000000);
//   }

//   // 1000000000
//   // call it get fiatConversionRate, since it assumes something about decimals
//   // It wouldn't work for every aggregator
//   function getConversionRate(uint256 ethAmount, AggregatorV3Interface priceFeed)
//     internal
//     view
//     returns (uint256)
//   {
//     uint256 ethPrice = getPrice(priceFeed);
//     uint256 ethAmountInUsd = (ethPrice * ethAmount) / 1000000000000000000;
//     // the actual ETH/USD conversation rate, after adjusting the extra 0s.
//     return ethAmountInUsd;
//   }
// }
